package com.perselab.recipe.model;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@Entity
@Table(name="recipe")
public class Recipe extends AbstractModel implements Auditable, Serializable{
	private static final long serialVersionUID = 1301969584123647562L;

	private Long id;
	private String name;
	private int serving;
	private String description;
	private Date creationDate;
	private Date modificationDate;
	private int status;
	
	private Set<Image> images;
	private Set<Ingredient> ingredients;
	private Set<Step> steps;
	private Set<Category> categories;
	
	/* (non-Javadoc)
	 * @see com.perselab.recipe.model.Model#getId()
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return this.id;
	}

	@Override
	@Column(name="creation_date")
	public Date getCreationDate() {
		return this.creationDate;
	}

	@Override
	@Column(name="modification_date")
	public Date getModificationDate() {
		return this.modificationDate;
	}
	
	@Override
	@Column
	public int getStatus() {
		return this.status;
	}

	@Column
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column
	public int getServing() {
		return serving;
	}

	public void setServing(int serving) {
		this.serving = serving;
	}

	@Column
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@OneToMany(mappedBy = "image", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	public Set<Image> getImages() {
		return images;
	}

	public void setImages(Set<Image> images) {
		this.images = images;
	}

	@OneToMany(mappedBy = "ingredient", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	public Set<Ingredient> getIngredients() {
		return ingredients;
	}

	public void setIngredients(Set<Ingredient> ingredients) {
		this.ingredients = ingredients;
	}

	@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	public Set<Step> getSteps() {
		return steps;
	}

	public void setSteps(Set<Step> steps) {
		this.steps = steps;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "category_recipe", joinColumns = { @JoinColumn(name = "recipe_id") }, inverseJoinColumns = @JoinColumn(name = "category_id"))
	@OrderBy("name")
	public Set<Category> getCategories() {
		return categories;
	}

	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public void setModificationDate(Date modificationDate) {
		this.modificationDate = modificationDate;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.append("id", this.id)
				.append("status", this.status)
				.append("name", this.name)
				.append("serving", this.serving)
				.append("description", this.description)
				.toString();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(-1683787035, -1518184133)
				.append(this.id)
				.append(this.status)
				.append(this.name)
				.append(this.serving)
				.append(this.description)
				.toHashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Role)) {
			return false;
		}

		final Recipe rhs = (Recipe) o;

		return new EqualsBuilder()
				.append(this.id, rhs.id)
				.append(this.status, rhs.status)
				.append(this.name, rhs.name)
				.append(this.serving, rhs.serving)
				.append(this.description, rhs.description)
				.isEquals();
	}

}
